<?php
/*
 * Copyright 2013 by Jerrick Hoang, Ivy Xing, Sam Roberts, James Cook, 
 * Johnny Coster, Judy Yang, Jackson Moniaga, Oliver Radwan, 
 * Maxwell Palmer, Nolan McNair, Taylor Talmage, and Allen Tucker. 
 * This program is part of RMH Homebase, which is free software.  It comes with 
 * absolutely no warranty. You can redistribute and/or modify it under the terms 
 * of the GNU General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
 * 
 */


/*
 * class Shift characterizes a time interval in a day new Shift
 * for scheduling volunteers
 * @version May 1, 2008, modified 9/15/08, 2/14/10
 * @author Allen Tucker and Maxwell Palmer
 * @author for ForestDale: Donald Barclay 4/16/14
 */

include_once(dirname(__FILE__).'/../database/dbShifts.php');
include_once(dirname(__FILE__).'/../domain/Appointment.php');

class Shift {

    private $clinicianID;   // String from Clincian
    private $mm_dd_yy;      // String: "mm-dd-yy".
    private $name;          // String: 'ss-ee' or 'overnight', where ss = start time and ee = end time e.g., '9-12'
    private $start_time;    // Integer: e.g. 10 (meaning 10:00am)
    private $end_time;      // Integer: e.g. 13 (meaning 1:00pm)
    private $day;         // string name of day "Monday"...
    private $id;            // "mm-dd-yy-ss-ee" is a unique key for this shift
    private $notes;  // notes written by the manager
    private $appointments;  //array of appointments
    private $isNewShift;

   
    /*
     * construct an empty shift with a certain number of vacancies
     */

    function __construct($cID,$mmddyy,$name,$startTime,$endTime,$day,$id,$notes,$shiftStatus) {
        $this->clinicianID = $cID;
        $this->mm_dd_yy = $mmddyy;
        $this->name = $name;
      //  $this->start_time = new DateTime("$st_h:$st_m:0");
     //   $this->end_time = new DateTime ("$et_h:$et_m:0");
        $this->start_time = $this->makeDateTime($startTime,$mmddyy);
        $this->end_time = $this->makeDateTime($endTime,$mmddyy);
        $this->day = $day;
        $this->id = $id;
        $this->notes = $notes;	
        $this->isNewShift = $shiftStatus;
        
        $appointments = array();
      //  $numOfAppointments = ($this->end_time.getdate() - $this->start_time.getdate())/15;
        $diffInSeconds = ($this->end_time->getTimestamp() - $this->start_time->getTimestamp());
        $diffInMinutes = $diffInSeconds/60;
        $numOfAppointments = $diffInMinutes/15;
       $apptStartTime = $this->start_time;   // first appt will start at shift start time
        if ($this->get_Shift_Status() == FALSE){
            error_log('this is a new shift');
            for ($i=0;$i<$numOfAppointments;++$i){     
                $apptStartTimeStr = $apptStartTime->format('H-i-s');
                error_log('the start time string is '.$apptStartTimeStr);
                $newAptID = $this->clinicianID.$this->mm_dd_yy.$apptStartTimeStr;
                $newAppt = new Appointment($newAptID,$this->clinicianID,null,null,null,null);//Last 2 nulls need to be StartTime and EndTime
                $appointments[$i] = $newAppt;
                
                // add 15 minutes to starting time
                $di = new DateInterval('PT15M');
                $apptStartTime->add($di);
            }
        $this->set_Shift_Status(TRUE);
        }
        else error_log('lin 77 shift.php' );
    }
   
    // convert from numeric hour and min, and a date string in
    // the format mm-dd-yy
    // to a DateTime object
     private function makeDateTime($timeStr,$mmddyy)
    {
        error_log('in Shift:makeDateTime, $timeStr is '.$timeStr.'  and mmddyy is '.$mmddyy);
        
        // split up the date string
        $parts= explode('-',$mmddyy);
        $month=$parts[0];
         $day=$parts[1];
        $year='20'.$parts[2];
        
        // split up the time string
        $timePieces = explode(':',$timeStr);
        $h = $timePieces[0];
        $m = $timePieces[1];
         $dtTime = new DateTime();
        $dtTime->setDate($year,$month,$day);
        $dtTime->setTime($h,$m,0);
        return $dtTime;
    }
    
    function set_start_time($st_h,$st_m) {
        $this->start_time = new DateTime("st_h:st_m:0");
    }
    function set_end_time($et_h,$et_m) {
        $this->end_time = new DateTime ("et_h:et_m:0");
    }
    
    /*
     * getters and setters
     */
    function get_clinicianID() {
        return $this->clinicianID;
    }
    function get_mm_dd_yy() {
    	return $this->mm_dd_yy;
    }
    function get_name() {
        return $this->name;
    }
    function get_start_time() {
        return $this->start_time;
    }
    function get_end_time() {
        return $this->end_time;
    }
    function get_day() {
        return $this->day;
    }
    function get_id() {
        return $this->id;
    }
    function get_notes() {
        return $this->notes;
    }   
    
    function get_Shift_Status(){
        return $this->isNewShift;
    }
    
    function set_mm_dd_yy($mm_dd_yy) {
        $this->mm_dd_yy = $mm_dd_yy;
    }
    function set_name($name) {
        $this->name = $name;
    }
    function set_day($day) {
        $this->day = $day;
    }
    function get_clinID(){
        return $this->clinicianID;
    }
    function set_notes($notes) {
        $this->notes = $notes;
    }  
    function set_Shift_Status($bool){
        $this->isNewShift = $bool;
    }
    function get_Appointments(){
        return $this->appointments;
    }
}

?>
